package guoanmaker.operator.business.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorCompanyDevelopServer;
import guoanmaker.operator.business.model.OperatorPcCompanyDevelop;
import guoanmaker.operator.business.model.OperatorServerClass;
import guoanmaker.operator.business.repository.OperatorCompanyDevelopServerRepository;
import guoanmaker.operator.business.repository.OperatorPcCompanyDevelopRepository;
import guoanmaker.operator.business.repository.OperatorServerClassRepository;

@Service
public class OperatorPcCompanyDevelopService implements OperatorPcCompanyDevelopServiceInterface {
	@Autowired
private OperatorCompanyDevelopServerRepository operatorCompanyDevelopServerRepository;
	@Autowired
	private OperatorPcCompanyDevelopRepository operatorPcCompanyDevelopRepository;
	@Autowired
	private OperatorServerClassRepository operatorServerClassRepository;
	 @PersistenceContext
		private EntityManager manager;
	@Override
	@Transactional
	public void saveOperatorPcCompanyDevelop(InRoundPhone inRoundPhone) throws Exception{
		// TODO Auto-generated method stub
		try{
		OperatorPcCompanyDevelop[] pds=inRoundPhone.getOperatorPcCompanyDevelops();
		
		for(OperatorPcCompanyDevelop hotserver:pds){
		hotserver.setCityId(inRoundPhone.getCityId());
		hotserver.setCreateDate(new Date());
		OperatorPcCompanyDevelop	 operatorPcCompanyDevelop=operatorPcCompanyDevelopRepository.save(hotserver);
		OperatorCompanyDevelopServer[] operatorCompanyDevelopServers=hotserver.getOperatorCompanyDevelopServers();
		 
			  String jpql="delete  from OperatorCompanyDevelopServer  u where u.comdepId=:comdepId";
			  Query query2=manager.createQuery(jpql);//查询商品提供的服务区域
				query2.setParameter("comdepId", hotserver.getId());
				query2.executeUpdate();
			  
		
		for(OperatorCompanyDevelopServer pdserver:operatorCompanyDevelopServers){
			if(pdserver.getTwoServerId()==null||pdserver.getTwoServerId().equals("")&&pdserver.getThreeServerId()==null||pdserver.getThreeServerId().equals("")){
				
				pdserver.setTrueServerId(pdserver.getOneServerId());
				
			}
			if(pdserver.getTwoServerId()!=null&&!pdserver.getTwoServerId().equals("")&&pdserver.getThreeServerId()==null||pdserver.getThreeServerId().equals("")){
				pdserver.setTrueServerId(pdserver.getTwoServerId());
			}
			if(pdserver.getTwoServerId()!=null&&!pdserver.getTwoServerId().equals("")&&pdserver.getThreeServerId()!=null&&!pdserver.getThreeServerId().equals("")){
				pdserver.setTrueServerId(pdserver.getThreeServerId());
			}
			pdserver.setComdepId(operatorPcCompanyDevelop.getId());
			
			operatorCompanyDevelopServerRepository.save(pdserver);
		}
	
		}
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		
	}
	@Override
	public Map selectOperatorPcCompanyDevelops(String cityId) {
		Map<String ,Object> remap=new HashMap<String ,Object>();
		// TODO Auto-generated method stub
		List<OperatorPcCompanyDevelop> opds=operatorPcCompanyDevelopRepository.getOperatorPcCompanyDevelops(cityId);
		for(OperatorPcCompanyDevelop opd:opds){
			OperatorCompanyDevelopServer[] depservers=operatorCompanyDevelopServerRepository.getOperatorCompanyDevelopServers(opd.getId());
			opd.setOperatorCompanyDevelopServers(depservers);
		}
		List<OperatorServerClass> serverTwos=operatorServerClassRepository.selectByTwoServerclass();
		List<OperatorServerClass> serverThrees=operatorServerClassRepository.selectByThreeServerclass();
		remap.put("opds", opds);
		remap.put("serverTwos", serverTwos);
		remap.put("serverThrees", serverThrees);
		return remap;
	}
	/**
	 * 官网调用企业发展
	 */
	@Override
	public List<OperatorPcCompanyDevelop> selectOperatorPcCompanyDevelopsAndserverName(String cityId) {
		// TODO Auto-generated method stub
		List<OperatorPcCompanyDevelop> opds=operatorPcCompanyDevelopRepository.getOperatorPcCompanyDevelops(cityId);
		for(OperatorPcCompanyDevelop opd:opds){
			OperatorCompanyDevelopServer[] depservers=operatorCompanyDevelopServerRepository.getOperatorCompanyDevelopServers(opd.getId());
			for(OperatorCompanyDevelopServer depserver:depservers){
				OperatorServerClass operatorServerClass=operatorServerClassRepository.findOne(depserver.getTrueServerId());
				depserver.setServerName(operatorServerClass.getServerName());
				depserver.setServerPic(operatorServerClass.getServerPicUrl());
				
			}
			opd.setOperatorCompanyDevelopServers(depservers);
		}
		
		return opds;
	}

}
